/* Copyright 203-2115 AtomWare Technology Pty Limited
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * 
 * The flowing information must be keep 
 * CleanUserJdbcWork.java
 * myapp
 * 2012-10-23下午2:22:12
 * @author 屈甲康
 * QQ285799123
 * mailto:qujiakang@gmail.com
 */

package org.jackysoft.myapp;

import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;

import javax.annotation.Resource;

import org.hibernate.SessionFactory;
import org.jackysoft.data.JdbcWork;
import org.springframework.stereotype.Component;

/**
 * 真正删除用户及其相关所有信息
 * */
@Component
public class CleanUserJdbcWork implements JdbcWork {
	private SessionFactory sessionFactory;
	private String user;
	private boolean forced = false;
	
	
	public CleanUserJdbcWork() {
		super();
		// TODO Auto-generated constructor stub
	}




	public CleanUserJdbcWork(String user,boolean forced) {
		super();
		this.user = user;
		this.forced = forced;
	}




	@Override
	public void execute(Connection connection) throws SQLException {
	   Statement stat = connection.createStatement();
	   //普通用户删除
       stat.addBatch("delete from UploadedFileItem where owner='"+user+"'");
       stat.addBatch("delete from ReceivedFileItem where owner='"+user+"'");
       stat.addBatch("update ReceivedFileItem set fromer=null where fromer='"+user+"'");
       stat.addBatch("delete from folder f where f.owner='"+user+"'");
       stat.addBatch("delete from user_file_owner where owners_name='"+user+"'");
       stat.addBatch("delete from user_file_reader where readers_name='"+user+"'");
       stat.addBatch("delete from user_file_readered where readereds_name='"+user+"'");
       stat.addBatch("delete from usersetting where owner.username='"+user+"'");
       if(forced){
    	   
       }
       //TODO 删除用户相关业务数据
       stat.addBatch("delete from user_authority user_id='"+user+"'");
       stat.addBatch("delete from sysuser where name='"+user+"'");
	}
	
	

	
	@Resource
	public void setSessionFactory(SessionFactory sessionFactory) {
		this.sessionFactory = sessionFactory;
	}


	@Override
	public SessionFactory getSessionFactory() {
	
		return sessionFactory;
	}

}
